from django.db import models
from django.utils import timezone

class Agent(models.Model):
    AGENT_TYPES = (
        ('输入型', '输入型'),
        ('输出型', '输出型'),
        ('对话型', '对话型'),
        ('工具型', '工具型'),
        ('网络型', '网络型'),
    )
    
    # title = models.CharField("名称", max_length=100)
    # description = models.TextField("描述")
    agent_type = models.CharField("类型", max_length=20, choices=AGENT_TYPES)
    config = models.JSONField("配置", default=dict, blank=True)
    created_at = models.DateTimeField("创建时间", default=timezone.now)
    updated_at = models.DateTimeField("更新时间", auto_now=True)

    def __str__(self):
        return f"{self.title} ({self.get_agent_type_display()})"

class Workflow(models.Model):
    name = models.CharField("名称", max_length=100)
    nodes = models.JSONField("节点数据")
    edges = models.JSONField("连接数据")
    created_at = models.DateTimeField("创建时间", default=timezone.now)
    updated_at = models.DateTimeField("更新时间", auto_now=True)

    def __str__(self):
        return f"{self.name} (ID: {self.id})"

class WorkflowExecution(models.Model):
    STATUS_CHOICES = [
        ('pending', '等待执行'),
        ('running', '执行中'),
        ('completed', '已完成'),
        ('failed', '已失败')
    ]
    
    workflow = models.ForeignKey(Workflow, on_delete=models.CASCADE)
    input_data = models.JSONField()
    output_data = models.JSONField(null=True)
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='pending')
    created_at = models.DateTimeField(auto_now_add=True)
    started_at = models.DateTimeField(null=True)
    completed_at = models.DateTimeField(null=True)